<template>
	<view>
		<view class="topquiz">
			<view class="top1" @click="top">
				上一题
			</view>
			<view class="jindu">
				<u-line-progress :percentage="percentage" activeColor="#49AAF5"></u-line-progress>
			</view>
			<view class="right">
				{{that}}<text>/{{all}}</text>
			</view>
		</view>
		<view class="contents">
			<view v-for="(element,index) in item" :key="index">
				<view v-if="that === index">
					<view class="title">
						{{index + 1}}.{{element.name}}

            <view v-if="element.url">
              <image :src="element.url" mode='aspectFit'  class="pic"></image>
            </view>
					</view>
					<view class="cons">
						<view @click="down(element.firstScore, 0)" v-if="element.a">
							{{element.a}}
						</view>
						<view @click="down(element.secondScore, 1)" v-if="element.b">
							{{element.b}}
						</view>
						<view @click="down(element.thirdScore, 2)" v-if="element.c">
							{{element.c}}
						</view>
						<view @click="down(element.forthScore, 3)" v-if="element.d">
							{{element.d}}
						</view>
						<view @click="down(element.fifthScore, 4)" v-if="element.e">
							{{element.e}}
						</view>
						<view @click="down(element.sixthScore, 5)" v-if="element.f">
							{{element.f}}
						</view>
						<view @click="down(element.seventhScore, 6)" v-if="element.g">
							{{element.g}}
						</view>
						<view @click="down(element.eighthScore)" v-if="element.h">
							{{element.h}}
						</view>
						<view @click="down(element.ninthScore)" v-if="element.i">
							{{element.i}}
						</view>
						<view @click="down(element.tenthScore)" v-if="element.j">
							{{element.j}}
						</view>
						<view @click="down(element.eleventhScore)" v-if="element.k">
							{{element.k}}
						</view>
						<view @click="down(element.twelfthScore)" v-if="element.l">
							{{element.l}}
						</view>
						<view @click="down(element.thirteenthScore)" v-if="element.m">
							{{element.m}}
						</view>
						<view @click="down(element.fourteenthScore)" v-if="element.n">
							{{element.n}}
						</view>
						<view @click="down(element.fifteenthScore)" v-if="element.o">
							{{element.o}}
						</view>

					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getTopicInfo
	} from '../../api/api.js'
	export default {
		data() {
			return {
				percentage: 0,
				that: 0,
				all: 20,
				item: [],
				answer: [], // 答案 分数
				answerChoose: [], // 答案 选择的索引
				id: null,
				score: 0,
				topicType: 0
			};
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id
				this.getTopic(option.id)
			}
			if(option.topicType) {
				this.topicType = option.topicType
			}
		},
		methods: {
			getTopic(topicId) {
				getTopicInfo({
					topicId: topicId
				}).then(res => {
					if (res.data.length !== 0) {
						this.all = res.data.length
						this.item = res.data
						this.test()
					}
				})
			},
			test() {
				let num = parseFloat(this.that / this.all).toFixed(2);
				this.percentage = (num * 100).toFixed(2)
			},
			down(e, index) {
				if (this.that === this.answer.length) {
					this.answer.push(e)
					this.answerChoose.push(index)
				} else {
					this.answer.splice(this.that, 1, e)
				}

				if (this.that + 1 < this.all) {
					this.that += 1
					this.test()
				} else {
					if (this.answer.length > 0) {
						this.score = this.answer.reduce((total, num) => total + num)
					}
					console.log("answer:", this.answer);
					console.log("topicType:", this.topicType);
					uni.redirectTo({
						// url: '/pages/userSide/testResult?id=' + this.id
						url: `/pages/userSide/testResult?id=${this.id}&score=${this.score}&answer=${JSON.stringify(this.answer)}&topicType=${this.topicType}&answerIndexs=${JSON.stringify(this.answerChoose)}`
					})
					
				}
			},
			top() {
				if (this.that >= 1) {
					this.that -= 1
					this.test()
				}
			}
		}

	}
</script>

<style lang="scss">
	.topquiz {
		padding: 60rpx 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;

		.top1 {
			background-color: #F3F3F3;
			padding: 10rpx 20rpx;
			color: #333;
			font-size: 28rpx;
			border-radius: 50rpx;
		}

		.jindu {
			width: 60vw;

		}

		.right {
			font-size: 32rpx;
			color: #333;

			text {
				font-size: 28rpx;
				color: #666;
			}
		}
	}

	.contents {
		width: 92vw;
		margin: 40rpx auto;
		background-color: #fff;
		padding: 0 30rpx 30rpx;

		.title {
			font-size: 36rpx;
			color: #333;
			padding: 35rpx 0;
		}

		.cons {
			view {
				margin-top: 30rpx;
				background-color: #EBEEF5;
				color: #333;
				font-size: 28rpx;
				padding: 30rpx 60rpx;
				text-overflow: ellipsis;
				overflow: hidden;
				white-space: nowrap;
				border-radius: 50rpx;
			}
		}
	}
</style>
